package com.example.hello.client.TabPanel;

import com.example.hello.BootstrapTabPanel;
import com.github.gwtbootstrap.client.ui.DropdownTab;
import com.github.gwtbootstrap.client.ui.TabLink;
import com.github.gwtbootstrap.client.ui.TabPane;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.Widget;
import com.vaadin.client.ComponentConnector;
import com.vaadin.client.communication.StateChangeEvent;
import com.vaadin.client.ui.AbstractComponentConnector;
import com.vaadin.shared.Connector;
import com.vaadin.shared.ui.Connect;

@Connect(BootstrapTabPanel.class)
public class BootstrapTabPanelConnector extends AbstractComponentConnector {

    @Override
    protected Widget createWidget() {
        return GWT.create(VBootstrapTabPanel.class);
    }

    @Override
    public BootstrapTabPanelState getState() {
        return (BootstrapTabPanelState) super.getState();
    }

    @Override
    public VBootstrapTabPanel getWidget() {
        return (VBootstrapTabPanel) super.getWidget();
    }

    @Override
    public void onStateChanged(StateChangeEvent stateChangeEvent) {
        super.onStateChanged(stateChangeEvent);

        BootstrapTabPanelState state = getState();

        VBootstrapTabPanel tabPanel = getWidget();

        for (Connector connector : state.getTabs()) {
            Widget widget = ((ComponentConnector) connector).getWidget();
            if (widget instanceof TabPane) {
                tabPanel.add((TabPane) widget);
            } else if (widget instanceof TabLink) {
                tabPanel.add((TabLink) widget);
            } else if (widget instanceof DropdownTab) {
                tabPanel.add((DropdownTab) widget);
            }
        }
        tabPanel.selectTab(state.getSelectedTab());
        tabPanel.setTabPosition(state.getTabPosition());
        tabPanel.setPullRight(state.isPullRight());
    }

}